package com.apes.fn.scm.masterdata;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.Request;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.product.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Title: MasterCategoryService
 * @Author fj
 * @Package com.apes.fn.scm.masterdata
 * @Date 2023/6/12 10:16
 * @description: 主数据-商品类目
 */
@Service("masterCategoryService")
public class MasterCategoryService extends DomainService {

    @Autowired
    private CategoryRepository categoryRepository;

    /**
     * 根据标准名称或俗称模糊查询
     * @param request
     * @return
     */
    public List findAllByClassifyNameOrName(SimpleRequest request){
        JSONObject requestJO = request.getJO();
        JSONObject data = requestJO.getJSONObject("data");
        String value = data.getString("keyword");
        List categoryList = categoryRepository.findAllByClassify_NameOrName(value);
        return MapUtil.mapped(categoryList);
    }

    /**
     * 根据标准名称或俗称精准查询
     * @param request
     * @return
     */
    public List findNames(SimpleRequest request){
        JSONObject requestJO = request.getJO();
        JSONObject data = requestJO.getJSONObject("data");
        String value = data.getString("keyword");
        List categoryList = categoryRepository.findByNames(value);
        return MapUtil.mapped(categoryList);
    }

    public JSONObject fullDecode(SimpleRequest request){
        JSONObject requestJO = request.getJO();
        String keyword = requestJO.getString("keyword");
        //若搜索关键字存在中文括号需转为英文括号
        String replace = keyword.replace("（", "(");
        String rKeyword = replace.replace("）", ")");

        //oe号不存在的情况到俗称和标准配件名称库查询
        Map datas = MapUtil.mapper("keyword", rKeyword);
        request.setData(MapUtil.mapper("data", datas));
        List categorys = findNames(request);
        requestJO.put("categorys",categorys);
        return requestJO;
    }

    public JSONObject test(SimpleRequest request){
        System.out.println(request.getJO().toJSONString());
        return request.getJO();
    }
}
